<template>
    <!--  <mescroll-body @down="downCallback" @init="mescrollInit" @up="upCallback">-->
    <!--    &lt;!&ndash;    <slot></slot>&ndash;&gt;设计的佛i圣诞节偶分 111222</mescroll-body-->
    <!--  >-->

    <mescroll-body
        v-if="type === 'body'"
        @down="downCallback"
        @init="mescrollInit"
        @up="upCallback"
        v-bind="{
            height: minHeight,
            ...$attrs.config as Object
        }"
    >
        <slot />
        <!--		<empty-data type="1" v-if="page.data && page.data.length === 0" />-->
    </mescroll-body>

    <mescroll-uni
        v-else
        @down="downCallback"
        @init="mescrollInit"
        @up="upCallback"
        v-bind="{
            height: minHeight,
            ...$attrs.config as Object
        }"
    >
        <slot />

        <!--    <empty-data type="1" v-if="page.data && page.data.length === 0" />-->
    </mescroll-uni>

    <!--    <mescroll-uni-->
    <!--        v-else-->
    <!--        :height="height"-->
    <!--        @down="downCallback"-->
    <!--        @init="mescrollInit"-->
    <!--        @up="upCallback"-->
    <!--        :down="{-->
    <!--      textInOffset: t('common.pullDownRefresh'),-->
    <!--      textOutOffset: t('common.releaseUpdate'),-->
    <!--      textLoading: t('common.loading'),-->
    <!--      textSuccess: t('common.loadSuccessfully'),-->
    <!--      textErr: t('common.loadFailure'),-->
    <!--    }"-->
    <!--        :up="{-->
    <!--      textLoading: t('common.loading'),-->
    <!--      textNoMore: t('common.textNoMore'),-->
    <!--    }"-->
    <!--        :empty="{-->
    <!--      tip: t('common.noMoreData'),-->
    <!--    }"-->
    <!--    >-->
    <!--        <slot />-->

    <!--        &lt;!&ndash;    <empty-data type="1" v-if="page.data && page.data.length === 0" />&ndash;&gt;-->
    <!--    </mescroll-uni>-->
</template>

<script setup lang="ts">
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll'
import { defaultOptions, mergeDefaultOptions } from '@/hooks/usePage'

const { mescrollInit, getMescroll } = useMescroll(onPageScroll, onReachBottom)

const props = defineProps({
    page: {
        type: Object,
        default: () => ({})
    },
    type: {
        type: String,
        default: 'body'
    },
    height: {
        type: String,
        defalut: '600'
    }
})
console.log('props.page', props.page)
// 安全区域高度
const minHeight = uni.getSystemInfoSync().safeArea.height + 'px'
// if (!props.page.createdIsNeed) {
//   props.page.fetchData = undefined
// }
console.log('安全区域高度', minHeight)

const emits = defineEmits([
    'resetData',
    'downBeforeCallback',
    'downCallback',
    'upCallback',
    'onGetListLafer',
    'triggerDownScroll'
])

const state = mergeDefaultOptions(defaultOptions, props.page)

// 重置数据
const resetData = () => {
    getMescroll().resetUpScroll()
    emits('resetData')
}
// 方法 开启下拉刷新
const triggerDownScrollFn = () => {
    getMescroll().triggerDownScroll()
    emits('triggerDownScroll')
}

// 下拉刷新的回调 (mixin默认resetUpScroll)
const downCallback = () => {
    emits('downBeforeCallback')
    if (getMescroll().optUp.use) {
        getMescroll().resetUpScroll()
        emits('downCallback')
    } else {
        setTimeout(() => {
            getMescroll().endSuccess()
        }, 100)
    }
}
// let flag = 1;

// 上拉加载的回调: 其中num:当前页 从1开始, size:每页数据条数,默认10
const upCallback = async (mescroll) => {
    // console.log('upCallback', mescroll.num, mescroll.size)

    // if (flag === 1 && !state.createdIsNeed) {
    //   return;
    // }
    // flag++;

    // 第一页需手动制空列表
    // if (mescroll.num == 1) {
    //   state.data = [];
    //   // 存在beforeLoadingFn方法则调用
    //   if (state.beforeLoadingFn) {
    //     state.beforeLoadingFn();
    //   }
    // }

    // 判断是否存在state.fetchData属性
    if (state.fetchData) {
        state.pagination!.current = mescroll.num
        state.pagination!.size = mescroll.size

        // 调用state.pageList方法发起分页查询
        const res = await state.fetchData({
            current: state.pagination?.current,
            size: state.pagination?.size,
            descs: state.descs?.join(','),
            ascs: state.ascs?.join(','),
            ...state.params
        })

        if (res.ok) {
            // 设置展示的数据数组
            // 第一页需手动制空列表
            if (mescroll.num == 1) {
                state.data = []
                // 存在beforeLoadingFn方法则调用
                if (state.beforeLoadingFn) {
                    state.beforeLoadingFn()
                }
            }
            nextTick(() => {
                state.data = state.data?.concat(
                    state.isPage ? res.data[state.props.item] : res.data
                )
                state.total = state.isPage ? res.data.total : 0
                // 设置分页信息中的总数据条数
                state.pagination!.total = state.isPage ? res.data[state.props.totalCount] : 0

                //方法一(推荐): 后台接口有返回列表的总页数 totalPage
                mescroll.endByPage(state.data?.length, res.data.pages) //必传参数(当前页的数据个数, 总页数)

                emits('upCallback')

                //方法二(推荐): 后台接口有返回列表的总数据量 totalSize
                // mescroll.endBySize(state.data?.length, state.pagination?.total); //必传参数(当前页的数据个数, 总数据量)

                emits('onGetListLafer')
            })
        } else {
            mescroll.endErr() // 请求失败, 结束加载
        }
    }
}

defineExpose({
    resetData,
    triggerDownScrollFn
})
</script>
<style scoped lang="scss">
* {
    flex-direction: column;
}
</style>
